import en from "./lang/en";
interface LanguageInner {
  [name: string]: string | string[] | LanguageInner;
}
interface Language {
  name: string;
  vb: LanguageInner;
  [name: string]: any;
}
let current: Language = en;
/**
 * t('vb.global.ok')
 */
export const t = function (keyStr: string, options?: any) {
  const keys: string[] = keyStr.split(".");
  let obj = current;
  let target;
  for (let i = 0, ii = keys.length; i < ii; i++) {
    const key: string = keys[i];
    target = obj[key];
    if (!target) return "";
    if (i === ii - 1) {
      if (!options) return target;
      return (target as string).replace(/\{(\w+)\}/g, (match, key) => {
        return options[key];
      });
    }
    obj = target;
  }
  return "";
};
export const set = function (lang: Language) {
  current = lang;
};
